package p2p.peer.command.visitor;

import p2p.logic.connection.Command;
import p2p.logic.connection.communication.message.CommandVisitor;
import p2p.logic.connection.communication.message.Message;
import p2p.peer.ClientManager;
import p2p.peer.command.BroadCast;
import p2p.peer.command.Connect;
import p2p.peer.command.Leave;
import p2p.peer.command.Spawn;

public class GSVisitor implements CommandVisitor{
	
	private Message m;
	private boolean isAck;
	private ClientManager manager;
	
	public GSVisitor(ClientManager manager, Message m, boolean isAck){
		
		this.isAck = isAck;
		this.manager = manager;
		this.m = m;
	
	}
	
	@Override
	public Command getCommand() {
		
		switch (this.m.getCommand()){
			
			case 0: return new Spawn(this, this.m, isAck); 
			case 1: return new Leave(this, this.m, isAck); 
			case 2: return new Connect(); 
			case 3: return new BroadCast(this, this.m, isAck);
			default: return null;
		}
	
	}

	@Override
	public int getCommandCode() {
		
		return this.m.getCommand();
	
	}

	@Override
	public Message getMessage() {
		return this.m;
	}

	@Override
	public void execute() {
		
		this.getCommand().execute();
		
	}
	
	public ClientManager getManager(){
		return this.manager;
	}
	
	
}
